package com.zhanghe.study.springmvc.deal;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author zh
 * @date 2022/3/31 10:47
 */
public class DealFile {

    public static void main(String[] args) {
        String jsonDir = System.getProperty("user.dir")+"/json";
        String version = "202203";
        dealDeviceBlackList(version,jsonDir);
        dealIpBlackList(version,jsonDir);
        dealCityCode(version,jsonDir);
        dealIp(version,jsonDir);
    }



    public static void dealDeviceBlackList(String version,String path){
        Gson gson = new Gson();
        try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("device_blacklist.log");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            BufferedWriter write = new BufferedWriter(new FileWriter(path+ "/caa_device_blacklist_"+version+".json"));){
            String line;
            while ((line = br.readLine()) != null) {
                DeviceBlack deviceBlack = new DeviceBlack();
                deviceBlack.setVersion(version);
                String[] cols = line.trim().split("\t");
                String deviceId = cols[0];
                String idType = cols[1];
                String col = cols[2];
                if(StringUtils.isNotBlank(col)){
                    String[] split = col.trim().split(":");
                    String encryption = split[0];
                    String publisher = split[1];
                    deviceBlack.setEncryption(encryption);
                    deviceBlack.setPublisher(publisher);
                }
                deviceBlack.setDeviceId(deviceId);
                deviceBlack.setIdType(idType);
                write.write(gson.toJson(deviceBlack));
                write.newLine();
                write.flush();

            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void dealIpBlackList(String version,String path){
        Gson gson = new Gson();
        try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("IP_blacklist.log");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            BufferedWriter write = new BufferedWriter(new FileWriter(path+ "/caa_ip_blacklist_"+version+".json"));){

            String line;
            while ((line = br.readLine()) != null) {
                IpBlack ipBlack = new IpBlack();
                ipBlack.setVersion(version);
                String[] cols = line.trim().split(":");
                String ip = cols[0];
                String publisher = cols[1];

                ipBlack.setIp(ip);
                ipBlack.setPublisher(publisher);
                write.write(gson.toJson(ipBlack));
                write.newLine();
                write.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void dealCityCode(String version,String path){
        Gson gson = new Gson();
        try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("citycode.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            BufferedWriter write = new BufferedWriter(new FileWriter(path+ "/caa_ip_citycode_"+version+".json"));){

            String line;
            while ((line = br.readLine()) != null) {

                CityCode cityCode = new CityCode();
                cityCode.setVersion(version);
                String[] cols = line.trim().split("\t");
                String citycode = cols[0];
                String aliascode = cols[1];
                String country = cols[2];
                String province = cols[3];

                cityCode.setCitycode(citycode);
                cityCode.setAliascode(aliascode);
                cityCode.setCountry(country);
                cityCode.setProvince(province);

                if(cols.length == 5){
                    String city = cols[4];
                    cityCode.setCity(city);
                }

                write.write(gson.toJson(cityCode));
                write.newLine();
                write.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void dealIp(String version,String path){
        Gson gson = new Gson();
        try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("caa_ipdb.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            BufferedWriter write = new BufferedWriter(new FileWriter(path+ "/caa_ip_ipdb_"+version+".json"));){

            String line;
            while ((line = br.readLine()) != null) {

                IpDb ipDb = new IpDb();
                ipDb.setVersion(version);
                String[] cols = line.trim().split("\t");
                String startIp = cols[0];
                String endIp = cols[1];
                String country = cols[2];
                String province = cols[3];
                String city = cols[4];
                String citycode = cols[5];

                ipDb.setStartIp(startIp);
                ipDb.setEndIp(endIp);
                ipDb.setCountry(country);
                ipDb.setProvince(province);
                ipDb.setCity(city);
                ipDb.setCitycode(citycode);
                write.write(gson.toJson(ipDb));
                write.newLine();
                write.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
